﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;

//CID 0861 //2011.04.11 //Begin
namespace InfoJetSoft.Service.Util
{
    class Base64TypeUtil
    {
        public static bool isBase64Field(Form form, XmlNode node)
        {
            if (form.Manifest.SchemaSet != null)
            {
                return isBase64Node(form, node);
            }
            else
            {
                return isBase64Component(form, node);
            }
        }

        public static bool isBase64Node(Form form, XmlNode node)
        {
            if (form.Manifest.SchemaSet != null)
            {
                ParsedPSVI parsedPSVI = ParsedPSVI.GetFullParsedPSVI(form, node);
                if (parsedPSVI != null && parsedPSVI.Type != null && parsedPSVI.Type.Equals("base64Binary"))
                {
                    return true;
                }
            }
            return false;
        }

        public static ParsedPSVI getBase64PSVI(Form form, XmlNode node)
        {
            if (form.Manifest.SchemaSet != null)
            {
                ParsedPSVI parsedPSVI = ParsedPSVI.GetFullParsedPSVI(form, node);
                if (parsedPSVI != null && parsedPSVI.Type != null && parsedPSVI.Type.Equals("base64Binary"))
                {
                    return parsedPSVI;
                }
            }
            return null;
        }

        public static bool isBase64Component(Form form, XmlNode node)
        {
            if (node is XmlElement)
            {
                XmlElement element = (XmlElement)node;
                foreach (XmlToEdit xmlToEdit in form.Manifest.XmlToEditList)
                {
                    if (xmlToEdit.Component.Equals(JetConstant.CMP_xImage) ||
                        xmlToEdit.Component.Equals(JetConstant.CMP_xFileAttachment))
                    {
                        string targetNodeName = ExpressionUtil.parseTargetNodeName(xmlToEdit.Item);
                        if (targetNodeName.Equals(element.Name))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
}
//CID 0861 //2011.04.11 //End